#coding: utf-8
import os
import tool
import config
class Log:
    tag_ex_log_url = config.save_root + 'tag/ex/log.txt'
    tag_normal_log_url = config.save_root + 'tag/normal/log.txt'
    ugoku_log_url = config.save_root + 'tag/ugoku/log.txt'
    error_ex_log_url = config.save_root + 'tag/ex/error_log.txt'
    error_normal_log_url = config.save_root + 'tag/normal/error_log.txt'
    error_log_url = config.save_root + 'tag/error_log.txt'
    def __init__(self):
        self.tool = tool.Tool()
        return

    def normal_log(self,tag,page):
        string = 'Tag:' + str(tag.encode('utf8')) + ': has downloaded to the ' + str(page) + ' page\n'
        if not os.path.exists(self.tag_normal_log_url):
            log = open(self.tag_normal_log_url, 'w')
            log.writelines(string)
            log.close()
        else:
            log = open(self.tag_normal_log_url, 'a')
            log.writelines(string)
            log.close()
        print string

    def ex_log(self,tag,page):
        string = 'Tag:' + str(tag.encode('utf8')) + ': has downloaded to the ' + str(page) + ' page\n'
        if not os.path.exists(self.tag_ex_log_url):
            log = open(self.tag_ex_log_url, 'w')
            log.writelines(string)
            log.close()
        else:
            log = open(self.tag_ex_log_url, 'a')
            log.writelines(string)
            log.close()
        print string

    def ugoku_log(self,tag,url):
        string = 'Tag:' + str(tag.encode('utf8')) + ': url:  ' + str(url) + '\n'
        if not os.path.exists(self.ugoku_log_url):
            log = open(self.ugoku_log_url, 'w')
            log.writelines(string)
            log.close()
        else:
            log = open(self.ugoku_log_url, 'a')
            log.writelines(string)
            log.close()
        print string

    def error_log(self, tag, pid, purl):
        string = 'Tag:' + str(tag.encode('utf8')) +' ID:' + str(pid) + ': url:  ' + str(purl) + '\n'
        if not os.path.exists(self.error_log_url):
            log = open(self.error_log_url,'w')
            log.writelines(string)
            log.close()
        else:
            log = open(self.error_log_url, 'a')
            log.writelines(string)
            log.close()
        print string

    def ugoko_log(self,tag,url):
        surl = self.tool.removesomething('http://www.pixiv.net' + url)
        string = 'Tag:' + str(tag.encode('utf8')) + ': url:  ' + str(surl) + '\n'
        if not os.path.exists(self.ugoku_log_url):
            log = open(self.ugoku_log_url, 'w')
            log.writelines(string)
            log.close()
        else:
            log = open(self.ugoku_log_url, 'a')
            log.writelines(string)
            log.close()
        print string
